/* freeircd free irc daemon
 * coded by triton and orium
 * getinfo.c - created on 27/09/2006
 */

#include <stdbool.h>
#include "types.h"
#include "misc.h"

#include "debug.h"

/**
 * Returns pointer to channel in list.
 *
 * @param pchans channels list
 * @param channame channel name
 *
 * @return pointer to channel if found, otherwise NULL
 */
CHANS *get_chan(CHANS *pchans, char *channame)
{
    while  (pchans != NULL)
    {
        if (compic(pchans->name,channame))
                return pchans;

        pchans=pchans->next;
    }

    return NULL;
}

/**
 * Returns user in channel user list.
 *
 * @param chan channel
 * @param nick nick name
 *
 * @return pointer to user in channel list if found, otherwise NULL
 */
struct chan_users_list *get_user_in_chan(CHANS *chan, char *nick)
{
    struct chan_users_list *pusers=chan->users;

    while  (pusers != NULL)
    {
        if (compic(pusers->user->nick,nick))
            return pusers;

        pusers=pusers->next;
    }

    return NULL;
}

/**
 * Returns the number of users in a given channel.
 *
 * @param chan pointer to channel
 *
 * @return integer with number of users
 */
long get_quant_user_in_chan(CHANS *chan)
{
    struct chan_users_list *pusers=chan->users;
    long counter=0;

    while  (pusers != NULL)
    {
        counter++;
        pusers=pusers->next;
    }

    return counter;
}

USERREG *get_raw_user_reg(USERREG *user_regs, char *nick)
{
    while (user_regs != NULL)
    {
        if (compic(user_regs->nick,nick))
            return user_regs;

        user_regs=user_regs->next;
    }

    return NULL;
}

struct user_chans_list *get_chan_from_user_list(struct user_chans_list *userchans, char *chan)
{
    while (userchans != NULL)
    {
        if (compic(userchans->chan->name,chan))
            return userchans;

        userchans=userchans->next;
    }

    return NULL;
}

CLIENTS *get_client(CLIENTS *clients, char *nick)
{
    while (clients != NULL)
    {
        if (compic(clients->nick,nick))
            return clients;

        clients=clients->next;
    }

    return NULL;
}

/**
 * Returns the number of channels a given nick is in.
 *
 * @param chan_list user channel list
 *
 * @return integer with number of channels
 */
int get_quant_chans_of_nick(struct user_chans_list *chan_list)
{
    int counter=0;

    while  (chan_list != NULL)
    {
        counter++;

        chan_list=chan_list->next;
    }

    return counter;
}

int get_quant_ausers_of_chan(struct access_users *ausers_list)
{
        int counter=0;

        while (ausers_list != NULL)
        {
                counter++;

                ausers_list=ausers_list->next;
        }

        return counter;
}

int get_auser_max_num(struct access_users *ausers_list)
{
        int max=0;

        while (ausers_list != NULL)
        {
                if (ausers_list->num > max)
                        max=ausers_list->num;

                ausers_list=ausers_list->next;
        }

        return max;
}

int get_quant_notes_of_nick(struct reg_notas *notes_list)
{
        int counter=0;

        while (notes_list != NULL)
        {
                counter++;

                notes_list=notes_list->next;
        }

        return counter;
}

int get_note_max_num(struct reg_notas *notes_list)
{
        int max=0;

        while (notes_list != NULL)
        {
                if (notes_list->num > max)
                        max=notes_list->num;

                notes_list=notes_list->next;
        }

        return max;
}

unsigned char *get_md5_hash_from_ircop(IRCOPS *ircops, char *user)
{
        while (ircops != NULL)
        {
                if (comp(ircops->username,user))
                        return ircops->md5_hash;

                ircops=ircops->next;
        }

        return NULL;
}
